package com.itbaizhan.springbootdemo4.filter;



import javax.servlet.*;
import java.io.IOException;

/*注册web组件，方式二
* 1，编写Filter
* 2，使用配置类注册servlet*/

public class SecondFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("进入second filter");
        filterChain.doFilter(servletRequest,servletResponse);
        System.out.println("结束 second filter");
    }

    @Override
    public void destroy() {

    }
}

/*编写filter

public class SecondFilter implements Filter {
  @Override
  public void init(FilterConfig filterConfig) throws ServletException { }

  @Override
  public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    System.out.println("进入Second Filter");
    filterChain.doFilter(servletRequest,servletResponse);
    System.out.println("离开Second Filter");
   }

  @Override
  public void destroy() { }
}
使用配置类注册filter

@Configuration
public class FilterConfig {
  @Bean
  public FilterRegistrationBean getFilterRegistrationBean(){
    // 注册filter组件
    FilterRegistrationBean bean = new FilterRegistrationBean(new SecondFilter());
    // 添加过滤路径
    bean.addUrlPatterns("/second");
    return bean;
   }
}*/